<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>删除子字符串的最大得分</title>
</head>
<body>
  <script>
    function fn(s, x, y) {
      // x：较大分数值，x1x2：较大分数值的对应字符串
      let x1, x2;
      x > y ? (x1 = "a", x2 = "b") : ([x, y] = [y, x], x1 = "b", x2 = "a");
      // i,j维护x1和x2字符的剩余个数
      let i = 0, j = 0, ans = 0;
      s += 'c';
      for (let c of s) {
        if (c == x1) i++;
        else if (c == x2)
          if (i) {
            ans += x;
            i--;
          }
          else j++;
        else {
          ans += Math.min(i, j) * y;
          i = j = 0;
        }
      }
      return ans;
    }
    console.log(fn('cdbcbbaaabab', 4, 5))
  </script>
</body>
</html>